// pages/usercenter/deliveryAdress/modifyAddress/modifyAddress.js
//腾讯位置服务
var QQMapWX = require("../../../../utils/qqmap-wx-jssdk.min");
var qqmapsdk;
var app = getApp()
var IP = app.globalData.IP
Page({
  /**
   * 页面的初始数据
   */
  data: {
    checked: '',
    error_message: '',
    id: '',
    username: '',
    telnumber: '',
    provincename: '',
    cityname: '',
    countyname: '',
    detailinfo: '',
    isdefault: '',
    //经度纬度
    longitude: "",
    latitude: "",
    //chooseLocation接口返回的值
    location: {},
    openid: wx.getStorageSync('openid')
  },

  onLoad(options) {


 
    //接收上一个页面传的地址信息参数
    this.setData({
      id: options.id,
      username: options.username,
      telnumber: options.telnumber,
      provincename: options.provincename,
      cityname: options.cityname,
      countyname: options.countyname,
      detailinfo: options.detailinfo,
      isdefault: options.isdefault
    })
    if (this.data.isdefault == 1) {
      this.setData({
        checked: true
      })
    } else {
      this.setData({
        checked:false
      })
    }
    // 实例化API核心类
    qqmapsdk = new QQMapWX({
      key: 'A4XBZ-K76R4-ENNU6-FEXPT-AGUN2-N5BGO'
    });

  },

  onChange(e) {
    // checked 状态更新
    this.setData({
      checked: e.detail,
    });
    if (e.detail == true) {
      this.setData({
        isdefault: 1
      })
    } else {
      this.setData({
        isdefault: 0
      })
    }

  },

  deleteaddress(){
    wx.showModal({
      title: '',
      content: '确认删除当前地址？',
      complete: (res) => {
        if (res.cancel) {
          
        }
    
        if (res.confirm) {
          wx.request({
            url: `http://${IP}:8080/user/deleteaddress/`+this.data.id,
          })
          wx.navigateBack()
        }
      }
    })
  
  },
  formSubmit(e) {
    wx.request({
      url: `http://${IP}:8080/user/UpdateAddress`,
      data: {
        id: this.data.id,
        openid: this.data.openid,
        username: this.data.username,
        telnumber: this.data.telnumber,
        provincename: this.data.provincename,
        cityname: this.data.cityname,
        countyname: this.data.countyname,
        detailinfo: this.data.detailinfo,
        isdefault: this.data.isdefault
      },
      success: res => {
        wx.navigateBack()
      }
    })
  },

  //选择位置
  chooseLocation() {
    wx.chooseLocation({
      success: res => {
        this.setData({

          detailinfo: res.name
        })


      },
      fail: res => {


      }
    })

  },

  /**
   * 生命周期函数--监听页面加载
   */


  //获取省份 城市 区县
  getLocation() {

    wx.showModal({
      title: '温馨提示',
      content: '是否允许获取您的位置信息',
      success: res => {
        if (res.confirm) {
          this.getlngandlat()
          console.log(res)
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })

  },
  /**
   * 获取经纬度
   */
  getlngandlat() {

    wx.getLocation({
      success: res => {
        console.log(res)
        this.setData({
            longitude: res.longitude,
            latitude: res.latitude
          },
          () => {
            this.lngAndlatToaddress();
          });

      }
    })
  },
  /**
   * 经纬度转换成地址
   */


  lngAndlatToaddress() {

    let that = this;
  
    //逆地址解析
    qqmapsdk.reverseGeocoder({
      location: {
        latitude: that.data.latitude,
        longitude: that.data.longitude
      },
      success: function (res) {
        console.log(res)
        let tempData = res.result.address_component;
        that.setData({
          provincename: tempData.province,
          cityname: tempData.city,
          countyname: tempData.district,
        });
      },
      fail: function (error) {

      },
      complete: function (res) {


      }
    })
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})